package com.cheung.shopkeeper.common.utils;

import java.math.BigDecimal;
import java.math.RoundingMode;

public class NumUtil {
	
	public static String format(Double num, int scale) {
		BigDecimal bigDecimal = new BigDecimal(num.toString());
		BigDecimal bd = bigDecimal.setScale(scale, RoundingMode.HALF_UP);
		return bd.toString();
	}
	
	public static int compare(Double num1, Double num2, int scale) {
		String s1 = format(num1, scale);
		String s2 = format(num2, scale);
		return s1.compareTo(s2);
	}
	
	public static int compareWithNull(Double num1, Double num2) {
		//return num1 == null ? num2 == null : num1.compareTo(num2);
		if (num1 == null && num2 == null) {
			return 0;
		} else if (num1 == null && num2 != null) {
			return -1;
		} else if (num1 != null && num2 == null) {
			return 1;
		} else {
			return num1.compareTo(num2);
		}
	}
	
	public static void main(String[] args) {
		Double d = 1.01910101d;
		String s = NumUtil.format(d, 3);
		System.out.println(s);
		Double dd = 1.01910102d;
		System.out.println(compare(d, dd, 2));
		System.out.println(compare(d, dd, 8));
		System.out.println(compare(null, null, 1));
	}
	
}
